package org.zachary.creditbusiness.hotel.util;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@RestController
public class OccupancyTestController {

    private final ResourceLoader resourceLoader;

    public OccupancyTestController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 测试预测接口
     *
     * 前端示例请求：
     * GET /test/occupancy
     *    ?hotelId=1
     *    &currentDateTime=2025-06-29 12:00:00
     *
     * @param hotelId      酒店 ID
     * @param currentDate  前端传回来的本地时间字符串，格式：yyyy-MM-dd
     */
    @GetMapping("/hotels/occupancy")
    public Map<String, Double> testOccupancy(
            @RequestParam int hotelId,
            @RequestParam String currentDate   // 前端直接传 "2023-01-15"
    ) throws Exception {
        // 从 classpath:static/hoteldata 目录取 baseDir
        File base = resourceLoader.getResource("classpath:static/hoteldata").getFile();
        return OccupancyPredictionUtil.occupancyPrediction(
                hotelId,
                currentDate,
                base.getAbsolutePath()
        );
    }
}